﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaskF4
{
   

    class test
    {
        public byte flag = 0;
        public double N;
        public long Rub;
        public double y;
        public double Kop;

        public byte r;
        public long n;
        public int mlrd;
        public int mlrd_d;
        public int mln;
        public int mln_d;
        public int th;
        public int th_d;
        public int ed;
        public string result;

        const string MLRD = "миллиард";
        const string MLN = "миллион";
        const string TH = "тысяч";
        const string A = "а ";
        const string I = "и ";
        const string OV = "ов ";
        const string NULL = " ";

        string[] massC = { " ","сто","двести", "триста", "четыреста", "пятьсот", "шестьсот", "семсот", "восемсот", "девятьсот"};
        string[] massB = { " ", " ", "двадцать", "тридцать", "сорок", "пятьдесят", "шестьдесят", "семьдесят", "восемьдесят", "девяносто" };
        string[] massBx = { "десять'", "одинадцать", "двенадцать", "тринадцать", "четырнадцать", "пятнадцать", "шестнадцать", "семнадцать",
                              "восемнадцать", "девятнадцать" };
        string[] massA = { " ", "один", "два", "три", "четыре", "пять", "шесть", "семь", "восемь", "девять" };
        string[] massTx = { "одна", "две" };

        public void First()
        {
            Random rend = new Random();
           // N = rend.Next(0,1999999999);
            N = 987654321098.99;

            Rub = (long)N;
            Console.WriteLine("N = " + N + " Rub = " + Rub);
            n = Rub;
            r = 0;
            do
            {
                n = n / 10;
                r++;
            }
            while (n != 0);
            if (r > 9 & r < 13)
                Mlrd();
            if (r > 6 & r < 10)
                Mln();
            if (r > 3 & r < 7)
                Th();
            if (r > 0 & r < 4)
            {
                flag = 8;
                Ed();
            }
                
        }
  
        public void Mlrd()
        {
            mlrd = (int)(Rub / 1000000000);
            mlrd_d = (int)(Rub % 1000000000);
            flag = 1;
            Ed();

        }
        public void Mln()
        {
            if (flag == 0)
            {
                mln = (int)(Rub / 1000000);
                mln_d = (int)(Rub % 1000000);
                flag = 2;
                Ed();
            }
            if (flag == 1)
            {
                mln = (mlrd_d/ 1000000);
                mln_d = (mlrd_d % 1000000);
                flag = 2;
                Ed();
            } 
        }
        public void Th()
        {
            if (flag == 0)
            {
                th = (int)(Rub / 1000);
                th_d = (int)(Rub % 1000);
                flag = 3;
                Ed();
            }
            if (flag == 2)
            {
                th = (mln_d / 1000);
                th_d = (mln_d % 1000);
                flag = 3;
                Ed();
            }
            if (flag == 4)
            {
                flag = 5;
                Ed();
            }
        }
        public string Ed()
        {
            int a = 0;
            int b = 0;
            int c = 0;

            if (flag == 0)
            {
                ed = (int)(Rub);
                a = ed % 10;
                b = ((ed % 100) - a)/10;
                c = ((ed % 1000) - b)/100;

                result += (massC[c] + " ");
                if (b == 1)
                {
                    result += (massBx[a] + " ");
                    a = 0;
                }
                else 
                {
                     result += (massB[b] + " ");
                }

                 result += (massA[a] + " ");               
                    flag = 9;
            }

            if (flag == 1)
            {
                ed = mlrd;
                a = ed % 10;
                b = ((ed % 100) - a) / 10;
                c = ((ed % 1000) - b) / 100;
                
                result += (massC[c] + " ");
                if (b == 1)
                {
                    result += (massBx[a] + " ");
                    a = 0;
                }
                else
                {
                    result += (massB[b] + " ");
                }

                result += (massA[a] + " ");
                if (a == 0 & b == 0 & c == 0)
                {
                    result += NULL;
                }
                else
                {
                    result += MLRD;
                }

                if (a == 1 & b != 1)
                {
                    result += NULL;
                }
              
                if (a > 1 & a < 5 & b != 1)
                {
                    result += A;
                }
               
                if (a > 4 & a < 10)
                {
                    result += OV;
                }
                if (a == 0 & (b != 0 | c != 0))
                {
                    result += OV;
                }
                Mln();
            }

            if (flag == 2)
            {
                ed = mln;
                a = ed % 10;
                b = ((ed % 100) - a) / 10;
                c = ((ed % 1000) - b) / 100;
                
                result += (massC[c] + " ");
                if (b == 1)
                {
                    result += (massBx[a] + " ");
                    a = 0;
                }
                else
                {
                    result += (massB[b] + " ");
                }

                result += (massA[a] + " ");
                if (a == 0 & b == 0 & c == 0)
                {
                    result += NULL;
                }
                else
                {
                    result += MLN;
                }
                
                if (a == 1 & b != 1)
                {
                    result += NULL;
                }
               
                if (a > 1 & a < 5 & b != 1)
                {
                    result += A;
                }
               
                if (a > 4 & a < 10)
                {
                    result += OV;
                }
                if (a == 0 & (b != 0 | c !=0))
                {
                    result += OV;
                }
                Th();
            }
            if (flag == 3)
            {
                flag = 4;
                ed = th;
                a = ed % 10;
                b = ((ed % 100) - a) / 10;
                c = ((ed % 1000) - b) / 100;
               
                if (c != 0)
                {
                    result += (massC[c] + " ");
                    if (a == 0 & b == 0)
                    {
                        result += TH;
                    }
                }
                if (b != 0 & b != 1)
                {
                    result += (massB[b] + " ");
                    if (a == 0 )
                    {
                        result += TH;
                        result += " ";
                    }
                }
                if (b == 1)
                {
                    result += (massBx[a] + " ");
                    result += TH;
                    a = 0;
                }
               

                if (a == 1 & b != 1)
                {
                    result += (massTx[0] + " ");
                    result += TH;
                    result += A;
                }
                if (a == 2 & b != 1)
                {
                    result += (massTx[a - 1] + " ");
                    result += TH;
                    result += I;
                }
                if (a > 2 & a < 5 & b != 1)
                {
                    result += (massA[a] + " ");
                        result += TH;
                        result += I;
                }
                
                if (a == 0 & b == 0 & c == 0)
                {
                    result += NULL;
                }
               
                if ((a != 1 & a != 2) & (a != 0 | b != 0 | c != 0))
                {
                    //result += TH;
                } 
                if (a > 4 & a < 10)
                {
                    result += (massA[a] + " ");
                    if (c == 0 & b == 0)
                    {
                        result += TH;
                    }
                }
                if (a == 0)
                {
                    result += NULL;
                }
              
                Th();
            }
            if (flag == 5)
            {
                ed = th_d;
                a = ed % 10;
                b = ((ed % 100) - a) / 10;
                c = ((ed % 1000) - b) / 100;
                result += (massC[c] + " ");
                if (b == 1)
                {
                    result += (massBx[a] + " ");
                    a = 0;
                }
                else
                {
                    result += (massB[b] + " ");
                }

                result += (massA[a] + " ");
                Console.WriteLine(result);
                flag = 9;
            }
            if (flag == 8)
            {
                ed = (int)Rub;
                a = ed % 10;
                b = ((ed % 100) - a) / 10;
                c = ((ed % 1000) - b) / 100;
                result += (massC[c] + " ");
                if (b == 1)
                {
                    result += (massBx[a] + " ");
                    a = 0;
                }
                else
                {
                    result += (massB[b] + " ");
                }

                result += (massA[a] + " ");
                Console.WriteLine(result);
                flag = 9;

            }
            if (flag == 9)
            {
                Console.ReadKey();
            }
            return result;
        }

    }
}
